﻿ //给文本框加事件，在获得焦点时高亮显示
sfFocus = function() {
    var sfEls = document.getElementsByTagName("INPUT");
    for (var i=0; i<sfEls.length; i++) {
        var obj = sfEls[i];
        if (obj.type.toUpperCase() == "TEXT")
        {
		    if (!obj.onfocus)
		    {
			    obj.onfocus=function() {
				    this.className+=" sffocus";
				    this.select();
			    }
		    }
            
            obj.onblur=function() {
                this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
            }
        }
    }
}
if (window.attachEvent) {window.attachEvent("onload", sfFocus);}
else if (window.addEventListener) {window.addEventListener("load", sfFocus, false);}

// 按箭头的上下键加减日期
function pdDate(obj,e)
{
    var keycode = 0;
    var isIE = false;
    
    // IE
    if (window.event) {
        keycode = e.keyCode;
        isIE = true;
    }
    else if (e.which) {
        keycode = e.which;
    }
    
    var dateStr = obj.value;
    var newDate;
    
    try{
        newDate = new Date();
        
        if (dateStr != "") {
            var tmp = dateStr.substring(0, 10);
            if (checkDate(tmp)) {
                var s = tmp.split("-");
                newDate = new Date(s[0], parseFloat(s[1]) - 1, parseFloat(s[2]));
            } 
            else {return;}
        }
        else
        {
            if (obj.withtime)
            {
                dateStr = "0000000000 " + newDate.getHours() + ":" + newDate.getMinutes();
            }
        }
        
        switch(keycode)
        {
            case 38 :
                newDate.setDate(newDate.getDate() + 1);
                break;
                
            case 40 :
                newDate.setDate(newDate.getDate() - 1);
                break;

			default :
				return;
        }
        
        var m = new String(newDate.getMonth() + 1);
        var d = new String(newDate.getDate());
        
        var result = newDate.getFullYear() + "-" + (m.length > 1 ? m : "0" + m) + "-" + (d.length > 1 ? d : "0" + d);
        
        obj.value = result + dateStr.substring(10);
    } catch(e) {
        return;
    }
}

// 验证日期格式：yyyy-MM-dd
function checkDate(theDate){
  var reg = /^\d{4}-((0{0,1}[1-9]{1})|(1[0-2]{1}))-((0{0,1}[1-9]{1})|([1-2]{1}[0-9]{1})|(3[0-1]{1}))$/; 
  var result=true;
  if(!reg.test(theDate))
    result = false;
  else{
    var arr_hd=theDate.split("-");
    var dateTmp;
    dateTmp= new Date(arr_hd[0],parseFloat(arr_hd[1])-1,parseFloat(arr_hd[2]));
    if(dateTmp.getFullYear()!=parseFloat(arr_hd[0])
       || dateTmp.getMonth()!=parseFloat(arr_hd[1]) -1
        || dateTmp.getDate()!=parseFloat(arr_hd[2])){
        result = false
    }
  }
  return result;
}